package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AdminMapper;
import com.itheima.dao.RoleMapper;
import com.itheima.entity.Result;
import com.itheima.pojo.Admin;
import com.itheima.pojo.Role;
import com.itheima.service.AdminService;
import com.itheima.utils.CheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl  implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 添加用户
     * @param admin
     */
    @Override
    public void add(Admin admin) {
        adminMapper.insertSelective(admin);
    }

    /**
     * 分配用户角色信息
     * @param adminUid
     * @return
     */
    @Override
    public Map<String, Object> assign(String adminUid) {
        Admin admin = adminMapper.selectByPrimaryKey(adminUid);
        if(admin==null){
            throw new RuntimeException("未查询到用户");
        }
        Role assignedRoles = roleMapper.selectByPrimaryKey(admin.getRoleUid());
        List<Role> unassignRoles = roleMapper.excludeByRoleId(admin.getRoleUid());
        Map<String,Object>resultMap=new HashMap<>();
        resultMap.put("unassignRoles",unassignRoles);
        resultMap.put("assignedRoles",assignedRoles);
        resultMap.put("admin",admin);
        return resultMap;
    }

    /**
     * 根据用户uid删除用户
     * @param adminUids
     */
    @Override
    public void deleteByAdminUids(String[] adminUids) {
        for (String adminUid : adminUids) {
            Admin admin = new Admin();
            admin.setUid(adminUid);
            admin.setStatus(0);
            adminMapper.updateByPrimaryKeySelective(admin);
        }
    }

    /**
     * 更新用户信息
     * @param admin
     */
    @Override
    public void edit(Admin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param keywords
     * @return
     */
    @Override
    public Page<Admin> findPage(Integer currentPage, Integer pageSize, String keywords) {
        PageHelper.startPage(currentPage,pageSize);
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("email","%"+keywords+"%");
        return (Page<Admin>)adminMapper.selectByExample(example);
    }

    /**
     * 重置管理员密码
     * @param uid
     */
    @Override
    public void restPwd(String uid) {
        Admin admin = new Admin();
        admin.setUid(uid);
        String gensalt = BCrypt.gensalt();
        String hashpw = BCrypt.hashpw("123456", gensalt);
        admin.setPassWord(hashpw);
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    /**
     * 修改手机号或者邮箱
     * @param uid
     * @param newInfo
     * @param validCode
     * @return
     */
    @Override
    public Result updateEmail(String uid, String newInfo, String validCode) {
        String code = (String) redisTemplate.opsForValue().get("SMS:" + newInfo);
        if(code==null|| !code.equals(validCode)){
            return new Result("200","验证码有误");
        }
        Admin admin = new Admin();
        admin.setUid(uid);
        if(CheckUtils.checkEmail(newInfo)){
            admin.setEmail(newInfo);
        }else {
            admin.setMobile(newInfo);
        }
        return new Result("200","修改成功");
    }

    @Override
    public Admin selectAdminByUsername(String name) {
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userName",name);
        List<Admin> admins = adminMapper.selectByExample(example);
        return admins.get(0);

    }


}
